#include "input.h"

input::input()
{
    //Set up  joystick
    if ( SDL_InitSubSystem ( SDL_INIT_JOYSTICK ) < 0 )
	{
		fprintf ( stderr, "Unable to initialize Joystick: %s\n", SDL_GetError() );
	}
    SDL_JoystickEventState(SDL_ENABLE);
    joystick1 = SDL_JoystickOpen(0);
    joystick2 = SDL_JoystickOpen(1);
    quit = false;
}

/* returns wheather the user quits or not
*/
bool input::perform_input(Levels *current_level)
{
//Get single key press from Keyboard
    SDL_Event event;
    while( SDL_PollEvent( &event ) )
    {
        switch(event.type)
        {
            case SDL_QUIT:
                    quit = true;
                break;
            case SDL_KEYDOWN:
                 switch(event.key.keysym.sym)
                 {
                        case SDLK_SPACE:
                            current_level->moveup();
                            break;
                        case SDLK_UP:
                            current_level->moveup();
                            break;
                        case SDLK_DOWN:
                            current_level->movedown();
                            break;
                        case SDLK_r:
                            current_level->attack();
                            break;
                        case SDLK_q:
                                quit = true;
                            break;

                        default:
                               break;
                 }
                 break;
            case SDL_MOUSEBUTTONDOWN:
                switch( event.button.button )
                {
                    case SDL_BUTTON_LEFT:
//                        cout << "button clicked!\n";
//                        cout << "event x: " << (double)(event.button.x) << endl;
//                        cout << "event y: " << (double)(event.button.y) << endl;
                        //current_level->attack( event );
                        break;
                    default:
                        break;
                }
                break;
            default:
                    break;
        }
    }

//Get Keyboard Input --constant
    keystate = SDL_GetKeyState(NULL);
    if (keystate[SDLK_LEFT] ) {
        current_level->moveleft();
    }
    if (keystate[SDLK_RIGHT] ) {
        current_level->moveright();
    }
    if (keystate[SDLK_s] ) {
        current_level->start_game();
    }


//////////////////Joystick////////////////////

//Joystick one get press set tokens Joystick 1
    if (SDL_JoystickGetButton(joystick1, 0) && !j1b0)
    {
       // current_level->attack();
        j1b0 = true;
    }
    if (SDL_JoystickGetButton(joystick1, 1) && !j1b1)
    {
        current_level->moveup();
        j1b1 = true;
    }
    if (SDL_JoystickGetButton(joystick1, 8))
        current_level->start_game();

//Joystick one get press set tokens Joystick 2
    if (SDL_JoystickGetButton(joystick2, 0) && !j2b0)
    {
        //current_level->attack();
        j2b0 = true;
    }
    if (SDL_JoystickGetButton(joystick2, 1) && !j2b1)
    {
        //current_level->moveup();
        j2b1 = true;
    }
    if (SDL_JoystickGetButton(joystick2, 8))
        current_level->start_game();


//Analog Direction Pad joystick 1
    int xmove = SDL_JoystickGetAxis(joystick1, 0);
    if( xmove > 12000)
        current_level->moveright();
    else if( xmove < -20000)
        current_level->moveleft();

//Analog Direction Pad joystick 2
    int xmove2 = SDL_JoystickGetAxis(joystick2, 0);
    if( xmove2 > 12000)
        current_level->moveright();
    else if( xmove2 < -20000)
        current_level->moveleft();




//Reset Joystick button tokens
    if (!SDL_JoystickGetButton(joystick1, 0))
        j1b0 = false;
    if (!SDL_JoystickGetButton(joystick1, 1))
        j1b1 = false;

    if (!SDL_JoystickGetButton(joystick2, 0))
        j2b0 = false;
    if (!SDL_JoystickGetButton(joystick2, 1))
        j2b1 = false;

    return quit;
}

input::~input()
{
    // close the joysticks
    SDL_JoystickClose(joystick1);
    SDL_JoystickClose(joystick2);
}
